package com.wizinno.android.urineflow.constant;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.wizinno.android.urineflow.activity.MainActivity;
import com.wizinno.android.urineflow.activity.SettingBaseActivity;
import com.wizinno.android.urineflow.data.ErrorCodeEnum;
import com.wizinno.android.urineflow.serialport.UsbHelper;
import com.wizinno.android.urineflow.util.LogUtils;

/**
 * 全局共享变量
 *
 * @author LiuMei
 * @date 2020-12-21.
 */
public enum CommonControl {
    INSTANCE;
    // 共享变量
    public Handler handler = null;
    public String DEVICE_SN = "";
    public String DEVICE_COMPANY = "";
    public MainActivity mainActivity = null;
    public SettingBaseActivity settingBaseActivity = null;
    public boolean isMqttNormal = false;

    //是否是管理员
    public boolean isAdmin = false;
    //存储授权
    public boolean isStoragePermission = false;

    /**
     * USB相关参数
     */
    public UsbHelper usbHelper = null;
    //USB通讯是否正常
    public boolean isUsbNormal = false;
    //USB是否插入
    public boolean isUsbIn = true;
    //USB是否已初始化
    public boolean isUsbInit = false;
    //是否是称重状态
    public boolean isWight = false;
    //A1版本
    public byte[] a1Version = null;
    //A2版本
    public byte[] a2Version = null;

    public static void usbInit() {
        INSTANCE.usbHelper = null;
        INSTANCE.a1Version = null;
        INSTANCE.a2Version = null;
        INSTANCE.isWight = false;
        INSTANCE.isUsbNormal = false;
        INSTANCE.isUsbIn = true;
        INSTANCE.isUsbInit = false;
    }

    public static void setA1Version(byte[] version) {
        INSTANCE.a1Version = version;
        checkVersion();
    }

    public static void setA2Version(byte[] version) {
        INSTANCE.a2Version = version;
        checkVersion();
    }

    public static void checkVersion() {
        if (INSTANCE.a1Version == null || INSTANCE.a2Version == null) {
            LogUtils.e(ErrorCodeEnum.USB_DEVICE_NET_ERROR.getDesc());
            INSTANCE.isUsbNormal = false;
            Message msg = new Message();
            Bundle data = new Bundle();
            msg.what = MessageInfo.WHAT_ERROR;
            data.putInt(MessageInfo.KEY_ERROR_CODE, ErrorCodeEnum.USB_DEVICE_NET_ERROR.getCode());
            msg.setData(data);
            INSTANCE.handler.sendMessage(msg);
        } else {
            INSTANCE.isUsbNormal = true;
        }
    }
}
